/**
 * Created by zhang on 2016/12/14.
 */
var dataManage = {
    events:{
        "#dataManageBody .copy-data click" : "copyData",
        "#dataManageBody .recover-data click" : "recoverData"
    },
    init: function () {
        var _this = this;
        document.title = "数据管理 - 系统管理";
        var saveTime = _this.getSaveTime();
        var temp = Util.template($("#dataManage-template").html(),{saveTime:saveTime});
        $(".commonBody-rightBody").html(temp);
        Util.event.call(_this,_this.events);
        _this.mouthSet();
    },
    getSaveTime : function(){
        var result=null;
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getSaveTime(),
            showLoading : false,
            async:false,
            timeout:60000,
            success : function(data){
                if(data.code == 200){
                    result = data.result;
                }  
            }
        });
        return result;
    },
    copyData : function(ele){
        var _this = this,
            type = $(ele).attr("type");
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.backup(),
            dataType: "json",
            data :{type : type},
            async:false,
            timeout:60000,
            success:function(data){
                if(data.code==200){
                    pop.dialog({
                        popTitle: "数据备份",
                        popContent: Util.template($("#copyData-template").html(), {}),
                        width :　"560px",
                        height :"400px",
                        footer:"<div class='pop-footer'>"+
                                    "<button class='btn btn-blue pop-ok' style='margin-right: 35px;' disabled>下载</button>"+
                                    "<button class='btn btn-gray pop-cancel'>取消</button>"+
                                "</div>",
                        onLoad: function () {
                            if (data.result.state == 1) {
                                $(".pop-ok").removeAttr("disabled");
                                $(".load-text").html("文件已生成！");
                            }else{
                                $(".load-text").html("文件正在生成中...");
                            }
                        }
                    },function(){
                        var para = "?type="+type+"&fileName="+data.result.fileName+"";
                        window.open(interfaceURL.download(para))
                    })
                }
            }
        });
    },
    recoverData : function(ele){
        var _this = this,
            type = $(ele).attr("type");
        pop.dialog({
            popTitle: "数据恢复",
            popContent: Util.template($("#recoverData-template").html(), {}),
            width :　"560px",
            height :"400px",
            footer:"<div class='pop-footer'>"+
                        "<button class='btn btn-blue pop-ok' style='margin-right: 35px;' disabled>恢复</button>"+
                        "<button class='btn btn-gray pop-cancel'>取消</button>"+
                    "</div>",
            onLoad: function () {
                var fileBtn = $("input#inputFile[type=file]");
                fileBtn.on("change", function(){
                    var uploadFileName = $(this).val();
                    _this.uploadFiles(uploadFileName);
                });
            }
        },function(){
            var fileName = $(".pop-ok").attr("fileName");
            Util.ajaxRequest({
                type: "get",
                url: interfaceURL.recovery(),
                dataType: "json",
                data :{
                    fileName : fileName,
                    type : type
                },
                async:false,
                timeout:60000,
                success:function(data){
                    if(data.code==200){
                        pop.alert("恢复成功！")
                    }
                }
            })
        })
    },
    uploadFiles: function (uploadFileName) {
        var _this = this;
        $("#fileForm").ajaxSubmit({
            type: 'post',
            url: interfaceURL.uploadFile(),
            autoError:false,
            success: function(data){
                if(data.code == 200){
                    $("#rightText").html(uploadFileName);
                    if (data.result.state == "1") {
                        $(".load-text").html("文件上传成功！");
                        $(".pop-ok").removeAttr("disabled").attr({"fileName":data.result.fileName});
                    }else{
                        $(".load-text").html("文件正在上传中...");
                    }
                }else{
                    pop.alert(data.message);
                }
            }
        })
    },
    mouthSet : function(){
        var mysel = $("#mouth-num");
        mysel.attr("last", mysel.val()).change(function () {
            var oldvalue = mysel.attr("last"),//这次改变之前的值
                newvalue = mysel.val(); //当前选中值
            pop.confirm("您的操作可能引起数据删除，确认继续吗？",function(){
                Util.ajaxRequest({
                    type: "get",
                    url: interfaceURL.saveMouthData(),
                    dataType: "json",
                    async:false,
                    timeout:60000,
                    data :{
                        months : newvalue
                    },
                    success:function(data){
                        if(data.code==200){
                            mysel.attr("last", newvalue);
                            pop.alert("设置成功！");
                        }
                    }
                });
            },function(){
                mysel.val(oldvalue);
            },function(){
                mysel.val(oldvalue);
            })
        })
    }
};